package com.ilop.sthome.ui.activity.device;

import android.text.TextUtils;
import android.util.Log;

import com.example.common.base.OnCallBackToRefresh;
import com.example.common.utils.InputFilterUtil;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.event.EventAnswerOK;
import com.ilop.sthome.mvp.contract.device.DeviceRenameContract;
import com.ilop.sthome.mvp.present.device.DeviceRenamePresenter;
import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.ilop.sthome.ui.dialog.BaseDialog;
import com.ilop.sthome.utils.CoderUtils;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityDeviceRenameBinding;

/**
 * @author skygge
 * @date 2020-02-18.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设备重命名
 */
public class DeviceRenameActivity extends ContainLoginActivity<DeviceRenamePresenter, ActivityDeviceRenameBinding> implements DeviceRenameContract.IView {

    private int mDeviceId;
    private String mDeviceName;
    private String mNickName;
    private String mOldName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_rename;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mDeviceId = getIntent().getIntExtra(CommonId.KEY_DEVICE_ID, 0);
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
        mOldName = getIntent().getStringExtra(CommonId.KEY_NICKNAME);
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new DeviceRenamePresenter(mContext);
    }

    @Override
    protected void initView() {
        super.initView();
        InputFilterUtil.setEditTextInhibitInputSpaChat(mContext, mDBind.etDeviceName);
        mDBind.tvRenameMsg.setText(mDeviceId == 0 ? getString(R.string.device_name_cannot_exceed_15_characters)
                : getString(R.string.device_name_cannot_exceed_15_characters_with_china));
        if (!TextUtils.isEmpty(mOldName)) mDBind.etDeviceName.setText(mOldName);
    }

    @Override
    protected void initData() {
        super.initData();
        mPresent.getDevice(mDeviceName, mDeviceId);
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.KEY_ANSWER_OK, EventAnswerOK.class).observe(this, event -> {
            if (event!=null){
                int result = CoderUtils.getAnswerResult(event.getData_str1(), event.getData_str2());
                Log.i(TAG, "initDataBus: answerOK--" + result);
                if (result == 5){
                    mPresent.renameSuccess(mNickName);
                }else if (result == 0){
                    showToast(getString(R.string.failed));
                }
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.tvSaveName.setOnClickListener(view -> onSaveNickName());
        mDBind.ivRenameBack.setOnClickListener(view -> whetherToSave());
        mDBind.ivRenameClear.setOnClickListener(v -> mDBind.etDeviceName.setText(""));
    }

    private void onSaveNickName(){
        mNickName = mDBind.etDeviceName.getText().toString();
        hideSoftKeyboard();
        showProgressDialog();
        if (mDeviceId == 0){
            mPresent.renameGateway(mNickName);
        }else if (mDeviceId == -1){
            mPresent.renameCamera(mNickName);
        }else{
            mPresent.renameSubDevice(mNickName);
        }
    }

    private void whetherToSave(){
        mNickName = mDBind.etDeviceName.getText().toString();
        if (!TextUtils.isEmpty(mOldName) && !mOldName.equals(mNickName)){
            BaseDialog mDialog = new BaseDialog(mContext, new OnCallBackToRefresh() {
                @Override
                public void onConfirm() {
                    onSaveNickName();
                }

                @Override
                public void onCancel() {
                    finish();
                }
            });
            mDialog.setMsg(getString(R.string.save_or_not));
            mDialog.show();
        }else {
            finish();
        }
    }

    /**
     * 修改成功
     */
    @Override
    public void onModifySuccess(){
        dismissProgressDialog();
        showToast(getString(R.string.modify_successfully));
        finish();
    }

    @Override
    public void showToastMsg(String msg) {
        dismissProgressDialog();
        showToast(msg);
    }

    @Override
    public void onBackPressed() {
        whetherToSave();
    }
}
